package com.tis.std;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import jpa.entitys.synchro.staging.SyncQueue;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import promis.utility.ProcessLevel;
import apps.promis.out.common.SyncQueueAction;
import com.tis.QueueCommon;
public class QueueProcessing extends QueueCommon {
	
	private static final Logger log = Logger.getLogger(QueueProcessing.class);
	public QueueProcessing() {}
	
	
	/**
	 * @param queue
	 * @throws Exception
	 * S = SUCCESS
	 * E = ERROR
	 * N = NON PROCESS
	 */
	public void ProcRun(SyncQueue queue, Connection conn) throws Exception {		
		SyncQueueAction SyncQueueAct = null;
		boolean isStartTime = false;
		boolean isStopTime = false;
		if(queue == null)  return;
		try{
		
			SyncQueueAct = new SyncQueueAction();
			
			//------------------------------------------------------------------
			isStartTime = true;
			isStopTime = false;
			SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.START_LEVEL, null);		
			//------------------------------------------------------------------
			
			isStartTime = false;
			isStopTime = false;
			
			JobsProcessing jobs = new JobsProcessing();
			jobs.setConn(conn);
			queue.setProcessCode("S");
			queue.setProcessDesc("SUCCESS");
			
			System.out.println("JobsId=" + queue.getQueueId() + " || ProcessingName=" +queue.getProcessingName() + " || Jobs Processing Time : " + new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss").format(Calendar.getInstance().getTime()));
			if(queue.getProcessingName().equals(promis.utility.ProcessModule.PROJECT.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.ProcProject(queue);
			}		
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.PROPERTY_UNIT.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.PropertyUnit(queue);
			}
				
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.CUSTOMER.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.Customer(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.BUDGET_HIGH_RISE.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.BudgetHighRise(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.CONSTRUCTION_CONTRACT_HIGH_RISE.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.ConsutuctionContractHighRise(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.REQUEST_FOR_PAYMENT_HIGH_RISE.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.RequestForPaymentHighRise(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.CONSTRUCTION_CONTRACT_LOW_RISE.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.ConsutuctionContractLowRise(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.GOODS_DIS_MATERIAL.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.GoodsDisMatrial(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.GOODS_RECEIVED.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.GoodsReceive(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.GOODS_RETURN.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				jobs.GoodsReturn(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.PURCHASE_REQUEST.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.PurchaseRequest(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.REQUEST_FOR_EXPENSE.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.RequestForExpense(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.REQUEST_FOR_PAYMENT.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.RequestForPayment(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SITE_MEMO.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SiteMemo(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SITE_MEMO_DEDUCTION.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SiteMemoDeduction(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SITE_MEMO_INTEGRATION.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SiteMemoIntegration(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.CREDIT_NOTE.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.RentalCreditNote(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.DEPOSIT_RETURN.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.RentalDepositReturn(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.INVOICE.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.RentalInvoice(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.RAINTAL_RECEIPT_MISC.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.RentalReceiptMisc(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.RAINTAL_RECEIPT.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.RentalInvoiceReceived(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.INVOICE_UNDUE.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.RentalInvoiceUndue(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SALES_BANK_REQUEST.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SalesBankDeduRequest(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SALES_BOOKING.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SalesBooking(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SALES_CREDIT_CARD_REQUEST.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SalesCerditRequest(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SALES_CONTRACT.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SalesContract(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SALES_CUSTOMER_REQUEST.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SalesCustomerRequest(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SALES_RECEIPT_ALL.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SalesReceiptAll(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SALES_RECEIPT_INSTALLMENT.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SalesReceiptInstallment(queue);
			}
			else if(queue.getProcessingName().equals(promis.utility.ProcessModule.SALES_TRANSFER.toString())){
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.PROCESSING_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.PROCESSING_LEVEL.toString());
				
				jobs.SalesTransfer(queue);
			}
			else
			{	
				isStartTime = false;
				isStopTime = true;
				SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.STOP_LEVEL, null);
				queue.setProcessLevel(ProcessLevel.STOP_LEVEL.toString());
				
				System.out.println("NON PROCESS JobsId=" + queue.getQueueId() + " || ProcessingName=" +queue.getProcessingName() + "Jobs Processing Time : " + new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss").format(Calendar.getInstance().getTime()));
				queue.setProcessCode("N");
				queue.setProcessDesc("NON PROCESS");
			}
		}catch(Exception e){
			e.printStackTrace();
			queue.setProcessCode("E");
			queue.setProcessDesc(CommonService.CutString(e.getMessage(), 2000));			
			System.out.println(e.getMessage());
		}finally{
			try{
				try{
					
					SyncQueueAct.UpdateSyncQueueCounter(queue);
					
					if(!queue.getProcessLevel().equals(ProcessLevel.STOP_LEVEL.toString())){
						isStartTime = false;
						isStopTime = true;
						SyncQueueAct.UPD_SYNCUseTime(queue.getQueueId(), conn, isStartTime, isStopTime, ProcessLevel.FINISH, null);
					}
					
				}catch(Exception e){
					e.printStackTrace();
					System.out.println(e.getMessage());
				}
				
				try{
					SyncQueueAct.MoveQueueToLogs(queue, conn);
				}catch(Exception e){
					e.printStackTrace();
					System.out.println(e.getMessage());
				}
			}finally{
				
			}
			
		}
	}
}
